#
# Copyright (c) 2012-2014 CESNET, z.s.p.o.
#
# LICENSE TERMS
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the Company nor the names of its contributors
#    may be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# ALTERNATIVELY, provided that this notice is retained in full, this
# product may be distributed under the terms of the GNU General Public
# License (GPL) version 2 or later, in which case the provisions
# of the GPL apply INSTEAD OF those given above.
#
# This software is provided ``as is'', and any express or implied
# warranties, including, but not limited to, the implied warranties of
# merchantability and fitness for a particular purpose are disclaimed.
# In no event shall the company or contributors be liable for any
# direct, indirect, incidental, special, exemplary, or consequential
# damages (including, but not limited to, procurement of substitute
# goods or services; loss of use, data, or profits; or business
# interruption) however caused and on any theory of liability, whether
# in contract, strict liability, or tort (including negligence or
# otherwise) arising in any way out of the use of this software, even
# if advised of the possibility of such damage.
#

NAME = @PACKAGE_NAME@
LNCTOOL = dev-tools/lnctool/lnctool

# Various configurable paths (remember to edit Makefile.in, not Makefile)
srcdir = @srcdir@
abs_srcdir = @abs_srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@
bindir = @bindir@
includedir = @includedir@
libdir =  @libdir@
mandir = @mandir@
readlink = @READLINK@
libtool = @libtool@

VERSION = $(shell cut -f1 "$(srcdir)/VERSION" | tr -d '\n')
# version for soname:
ABICOMPAT_VER = $(shell echo $(VERSION) | cut -d. -f1,2)
RELEASE = @RELEASE@
# libtool version info
version_info = @LIBTOOL_VERSION_INFO@

CC = @CC@
DOXYGEN = @DOXYGEN@
DOXYGEN_CONFIG = Doxyfile
DOXYGEN_DIR = $(shell cat $(DOXYGEN_CONFIG) | grep '^OUTPUT_DIRECTORY' | sed 's/.*=//')
RPMBUILD = @RPMBUILD@
LDCONFIG = ldconfig
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
RPMDIR = RPMBUILD
NC_WORKINGDIR_PATH = $(DESTDIR)/@NC_WORKINGDIR_PATH@
SETBIT = @SETBIT@
SETUSER = @SETUSER@
SETGROUP = @SETGROUP@
IDGIT = "built from git $(shell git show --pretty=oneline | head -1 | cut -c -20)"
IDNOGIT = "released as version $(VERSION)"
LIBS = @LDFLAGS@ @LIBS@
CFLAGS = -Wall @CFLAGS@
CPPFLAGS = -DNC_WORKINGDIR_PATH=\"$(NC_WORKINGDIR_PATH)\" -DSETBIT=$(SETBIT) @CPPFLAGS@
LIBTOOL = $(libtool) --tag=CC --quiet
HAVE_PYANG = @HAVE_PYANG@
HAVE_XSLTPROC = @HAVE_XSLTPROC@

PYTHON = @PYTHON@
ifdef PYTHON
PYAPI = pyapi
TARBALLPYAPI = tarball-pyapi
TARBALLPYAPIDIR = python3-netconf-$(VERSION)
INSTALLPYAPI = install-pyapi
UNINSTALLPYAPI = uninstall-pyapi
DOXYGEN_CONFIG_PYTHON = Doxyfile.python
PYPREFIX = @PYPREFIX@
PYHDRS = doc/python/netconf.py
PYSRCS = python/netconf.c \
	python/session.c
endif

INCLUDES= -I$(abs_srcdir)/src

ifeq "$(shell pwd)" "$(shell $(readlink) -f $(srcdir))"
OBJDIR = .obj
else
OBJDIR = .
VPATH = $(srcdir)
endif

SRCS =  src/callbacks.c \
	src/error.c \
	src/internal.c \
	src/compat.c \
	src/messages.c \
	src/session.c \
	src/transport.c \
	@SRCS_TRANSPORT@ \
	@SRCS_NOTIFICATIONS@ \
	src/with_defaults.c \
	@SRCS_URL@ \
	src/nacm.c \
	src/datastore.c \
	src/datastore/edit_config.c \
	src/datastore/empty/datastore_empty.c \
	src/datastore/file/datastore_file.c \
	src/datastore/custom/datastore_custom.c \
	src/transapi/transapi.c \
	src/transapi/yinparser.c \
	src/transapi/xmldiff.c

HDRS_PUBL_ROOT = headers/libnetconf.h \
		 headers/libnetconf_ssh.h \
		 @HDRS_PUBL_TLS@ \
		 headers/libnetconf_xml.h

HDRS_PUBL_SUBDIR = src/netconf.h \
	src/callbacks.h \
	src/callbacks_ssh.h \
	src/error.h \
	src/session.h \
	src/messages.h \
	src/messages_xml.h \
	src/transport.h \
	src/callhome.h \
	@HDRS_PUBL_SUBDIR_NOTIFICATIONS@ \
	src/with_defaults.h \
	@HDRS_PUBL_SUBDIR_URL@ \
	src/datastore.h \
	src/datastore_xml.h \
	src/datastore/custom/datastore_custom.h \
	src/transapi.h

HDRS_PUBL = $(HDRS_PUBL_ROOT) $(HDRS_PUBL_SUBDIR)
SUBHEADERS_DIR = libnetconf

HDRS_PRIV = src/config.h.in \
	src/libnetconf.h \
	src/libnetconf_xml.h \
	src/libnetconf_ssh.h \
	@HDRS_PRIV_TLS@ \
	src/callbacks.h \
	src/callbacks_ssh.h \
	src/error.h \
	src/messages.h \
	src/messages_internal.h \
	src/messages_xml.h \
	src/netconf.h \
	src/netconf_internal.h \
	src/session.h \
	src/transport.h \
	@HDRS_PRIV_TRANSPORT@ \
	src/callhome.h \
	@HDRS_PRIV_NOTIFICATIONS@ \
	src/with_defaults.h \
	@HDRS_PRIV_URL@ \
	src/nacm.h \
	src/datastore.h \
	src/datastore/datastore_internal.h \
	src/datastore/edit_config.h \
	src/datastore/empty/datastore_empty.h \
	src/datastore/file/datastore_file.h \
	src/datastore/custom/datastore_custom.h \
	src/datastore/custom/datastore_custom_private.h \
	src/transapi/transapi_internal.h \
	src/transapi/yinparser.h \
	src/transapi/xmldiff.h

GENERATOR_FILES = dev-tools/lnctool/generator/Makefile.in \
	dev-tools/lnctool/generator/configure.in \
	dev-tools/lnctool/generator/install-sh \
	dev-tools/lnctool/generator/config.guess \
	dev-tools/lnctool/generator/config.sub \
	dev-tools/lnctool/generator/ltmain.sh \
	dev-tools/lnctool/generator/README

LNCDS_FILES = dev-tools/lncdatastore/main.c \
	dev-tools/lncdatastore/commands.c \
	dev-tools/lncdatastore/commands.h \
	dev-tools/lncdatastore/mreadline.c \
	dev-tools/lncdatastore/mreadline.h \
	dev-tools/lncdatastore/Makefile.in

XML_SRCS = models/ietf-netconf-acm-data.rng.in

BUILT_RNGS = $(XML_SRCS:models/%.rng.in=models/%.rng)

models/%.rng: models/%.rng.in
	@sed -e "s/\@DATAROOTDIR\@/$(subst /,\/,$(datarootdir))/g" $< > $@

LOBJS = $(SRCS:%.c=$(OBJDIR)/%.lo)

all: $(NAME).la $(LNCTOOL) $(BUILT_RNGS) $(PYAPI)

$(NAME).la: $(LOBJS)
	@rm -f $@
	$(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) -version-number $(version_info) -no-undefined -o $@ $^ -rpath $(libdir)

.PHONY: $(LNCTOOL)
$(LNCTOOL): $(LNCTOOL).in
	@if test -n "$(HAVE_PYANG)" -a -n "$(HAVE_XSLTPROC)"; then \
		sed "s/@RNGLIB@/$(subst /,\/,@LNCTOOLDESTDIR@$(datadir)/$(NAME)/rnglib/)/g" $< | \
		sed 's/@XSLTDIR@/$(subst /,\/,@LNCTOOLDESTDIR@$(datadir)/$(NAME)/xslt/)/g' | \
		sed 's/@TEMPLATEDIR@/$(subst /,\/,@LNCTOOLDESTDIR@$(datadir)/$(NAME)/templates/)/g' > $(LNCTOOL).install; \
		sed "s/@RNGLIB@/$(subst /,\/,$(abs_srcdir)/dev-tools/lnctool/rnglib/)/g" $< | \
		sed 's/@XSLTDIR@/$(subst /,\/,$(abs_srcdir)/dev-tools/lnctool/xslt/)/g' | \
		sed 's/@TEMPLATEDIR@/$(subst /,\/,$(abs_srcdir)/dev-tools/lnctool/generator/)/g' > $(LNCTOOL); \
		chmod +x $(LNCTOOL) $(LNCTOOL).install; \
	fi

.PHONY: pyapi
pyapi: $(PYSRCS)
	@cd python; $(PYTHON) ./setup.py build; cd -;

$(OBJDIR)/%.lo: %.c
	@[ -d $$(dirname $@) ] || \
		(mkdir -p $$(dirname $@))
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) $(DBG) -fPIC -c $< -o $@

.PHONY: doc
doc: doc/doxygen/html/index.html

doc/doxygen/html/index.html: $(DOXYGEN_CONFIG) $(HDRS_PRIV) $(DOXYGEN_CONFIG_PYTHON) $(PYHDRS)
	@if [ "$(DOXYGEN)" != "no" ]; then \
		[ -d $(DOXYGEN_DIR) ] || mkdir -p $(DOXYGEN_DIR); \
		$(DOXYGEN); \
		if [ -n "$(DOXYGEN_CONFIG_PYTHON)" ]; then \
			$(DOXYGEN) $(DOXYGEN_CONFIG_PYTHON); \
		fi; \
	else \
		echo "Missing doxygen!"; \
	fi;

.PHONY: dist
dist: $(NAME).spec tarball rpm

.PHONY: tarball
tarball: $(SRCS) $(HEADERS)
	@rm -rf $(NAME)-$(VERSION);
	@mkdir $(NAME)-$(VERSION);
	for i in $(SRCS) $(HDRS_PUBL) $(HDRS_PRIV) configure.in configure \
	    headers/libnetconf.h.in headers/libnetconf_xml.h.in headers/libnetconf_ssh.h.in \
	    ltmain.sh Makefile.in VERSION $(NAME).spec.in $(NAME).pc.in $(LNCDS_FILES)\
	    dev-tools/lnctool/lnctool.in dev-tools/lnctool/rnglib/* dev-tools/lnctool/xslt/* dev-tools/lnctool/generator/*\
	    install-sh config.sub config.guess Doxyfile.in doc/img/*.png models/*; do \
	    [ -d $(NAME)-$(VERSION)/$$(dirname $$i) ] || (mkdir -p $(NAME)-$(VERSION)/$$(dirname $$i)); \
		cp $$i $(NAME)-$(VERSION)/$$i; \
	done;
	@rm -rf $(RPMDIR)/SOURCES/; \
	mkdir -p $(RPMDIR)/SOURCES/;
	tar -c -z -f $(RPMDIR)/SOURCES/$(NAME)-$(VERSION)-$(RELEASE).tar.gz \
	    $(NAME)-$(VERSION); \
	rm -rf $(NAME)-$(VERSION);

.PHONY: tarball-pyapi
tarball-pyapi: tarball
	@rm -rf $(TARBALLPYAPIDIR);
	@mkdir $(TARBALLPYAPIDIR);
	for i in python/netconf.h python/netconf.c python/session.c python/setup.py; do \
		cp $$i $(TARBALLPYAPIDIR)/$$(basename $$i); \
	done;
	@[ -d "$(RPMDIR)/SOURCES/" ] || (mkdir -p $(RPMDIR)/SOURCES/);
	tar -c -z -f $(RPMDIR)/SOURCES/$(TARBALLPYAPIDIR)-$(RELEASE).tar.gz $(TARBALLPYAPIDIR); \
	rm -rf $(TARBALLPYAPIDIR);

.PHONY: rpm
rpm: tarball $(TARBALLPYAPI)
	if [ "$(RPMBUILD)" != "no" ]; then \
		mkdir -p $(RPMDIR)/BUILD $(RPMDIR)/RPMS $(RPMDIR)/SRPMS; \
		$(RPMBUILD) -ba $(NAME).spec \
			--define "_topdir `pwd`/$(RPMDIR)"; \
		if [ -n "$(PYAPI)" ]; then \
			$(RPMBUILD) -ba python/python3-netconf.spec \
				--define "_topdir `pwd`/$(RPMDIR)"; \
		fi \
	else \
		echo "Missing rpmbuild"; \
		exit 1; \
	fi

.PHONY: install
install: all install-devel install-doc $(INSTALLPYAPI)
	$(INSTALL) -m 775 -d $(DESTDIR)/$(libdir)
	$(LIBTOOL) --mode=install cp $(NAME).la $(DESTDIR)/$(libdir)
	$(LIBTOOL) --mode=finish $(DESTDIR)/$(libdir)
	@if test "`id -u`" = "0"; then $(LDCONFIG); fi
	[ -d "$(NC_WORKINGDIR_PATH)" ] || \
		mkdir -p $(NC_WORKINGDIR_PATH)
	chmod 700 $(NC_WORKINGDIR_PATH)
	$(INSTALL_DATA) models/ietf-netconf-acm*.rng models/ietf-netconf-acm-schematron.xsl $(NC_WORKINGDIR_PATH)/
	$(INSTALL_DATA) models/yin2yang.xsl $(NC_WORKINGDIR_PATH)/
	@if test "$$(($(SETBIT) & 1))" = "1"; then \
		if test -n "$(SETUSER)"; then \
			chown $(SETUSER) $(NC_WORKINGDIR_PATH) || \
				echo "WARNING: invalid group $(SETUSER)"; \
		fi; \
	fi; \
	if test "$$(($(SETBIT) & 2))" = "2"; then \
		chmod g+rwx $(NC_WORKINGDIR_PATH); \
		if test -n "$(SETGROUP)"; then \
			chown :$(SETGROUP) $(NC_WORKINGDIR_PATH) || \
				echo "WARNING: invalid group $(SETGROUP)"; \
		fi; \
	fi; \
	if test "$(SETBIT)" = "0"; then \
		chmod a+rwx $(NC_WORKINGDIR_PATH); \
	fi;

.PHONY: install-devel
install-devel: $(LNCTOOL) $(NAME).la $(HDRS_PUBL_ROOT) $(HDRS_PUBL_SUBDIR) $(NAME).pc
	@[ -d $(DESTDIR)/$(libdir) ] || \
	    (mkdir -p $(DESTDIR)/$(libdir); chmod 755 $(DESTDIR)/$(libdir)); \
	[ -d $(DESTDIR)/$(libdir)/pkgconfig ] || \
	    (mkdir -p $(DESTDIR)/$(libdir)/pkgconfig; chmod 755 $(DESTDIR)/$(libdir)/pkgconfig); \
	[ -d $(DESTDIR)/$(includedir) ] || \
	    (mkdir -p $(DESTDIR)/$(includedir); chmod 755 $(DESTDIR)/$(includedir)); \
	[ -d $(DESTDIR)/$(includedir)/$(SUBHEADERS_DIR) ] || \
		(mkdir -p $(DESTDIR)/$(includedir)/$(SUBHEADERS_DIR)); \
	[ -d $(DESTDIR)/$(datadir)/$(NAME)/rnglib ] || \
		(mkdir -p $(DESTDIR)/$(datadir)/$(NAME)/rnglib); \
	[ -d $(DESTDIR)/$(datadir)/$(NAME)/xslt ] || \
		(mkdir -p $(DESTDIR)/$(datadir)/$(NAME)/xslt);
	[ -d $(DESTDIR)/$(datadir)/$(NAME)/templates ] || \
		(mkdir -p $(DESTDIR)/$(datadir)/$(NAME)/templates);
	$(LIBTOOL) --mode=install cp $(NAME).la $(DESTDIR)/$(libdir); \
	for i in $(HDRS_PUBL_ROOT); do \
		$(INSTALL_DATA) $(srcdir)/$$i $(DESTDIR)/$(includedir)/$$(basename $$i); \
	done; \
	for i in $(HDRS_PUBL_SUBDIR); do \
		$(INSTALL_DATA) $(srcdir)/$$i $(DESTDIR)/$(includedir)/$(SUBHEADERS_DIR)/$$(basename $$i); \
	done;
	$(INSTALL_DATA) $(NAME).pc $(DESTDIR)/$(libdir)/pkgconfig/$(NAME).pc;
	[ -d $(DESTDIR)/$(bindir) ] || \
	   	(mkdir -p $(DESTDIR)/$(bindir); chmod 755 $(DESTDIR)/$(bindir)); \
	$(INSTALL_PROGRAM) $(LNCTOOL).install $(DESTDIR)/$(bindir)/lnctool; \
	$(INSTALL_PROGRAM) $(DESTDIR)/$(bindir)/lncdatastore; \
	$(INSTALL_DATA) dev-tools/lnctool/rnglib/* $(DESTDIR)/$(datadir)/$(NAME)/rnglib/; \
	$(INSTALL_DATA) dev-tools/lnctool/xslt/* $(DESTDIR)/$(datadir)/$(NAME)/xslt/;
	$(INSTALL_DATA) dev-tools/lnctool/generator/* $(DESTDIR)/$(datadir)/$(NAME)/templates/;

.PHONY: install-doc
install-doc: doc
	[ -d $(DESTDIR)/$(datadir)/$(NAME)/doxygen ] || \
		(mkdir -p $(DESTDIR)/$(datadir)/$(NAME)/doxygen; \
		chmod -R 755 $(DESTDIR)/$(datadir)/$(NAME))
	cp -r $(DOXYGEN_DIR)/* $(DESTDIR)/$(datadir)/$(NAME)/doxygen/;

.PHONY: install-pyapi
install-pyapi: pyapi
	@cd python; $(PYTHON) ./setup.py install --prefix=$(DESTDIR)/$(PYPREFIX); cd -;

.PHONY: uninstall
uninstall: uninstall-devel uninstall-doc $(UNINSTALLPYAPI)
	$(LIBTOOL) --mode=uninstall rm -f $(DESTDIR)/$(libdir)/libnetconf.la
	[ ! -d $(DESTDIR)/$(libdir) ] || rmdir -p $(DESTDIR)/$(libdir) || echo "Directory already gone."
	rm $(NC_WORKINGDIR_PATH)/ietf-netconf-acm*.rng $(NC_WORKINGDIR_PATH)/ietf-netconf-acm-schematron.xsl;

.PHONY: uninstall-devel
uninstall-devel:
	$(LIBTOOL) --mode=uninstall rm -f $(DESTDIR)/$(libdir)/libnetconf.la \
	[ ! -d $(DESTDIR)/$(libdir) ] || rmdir -p $(DESTDIR)/$(libdir) || echo "Directory already gone."
	for i in $(HDRS_PUBL_ROOT); do \
		rm -f $(DESTDIR)/$(includedir)/$$(basename $$i); \
	done; \
	for i in $(HDRS_PUBL_SUBDIR); do \
		rm -f $(DESTDIR)/$(includedir)/$(SUBHEADERS_DIR)/$$(basename $$i); \
	done; \
	rm -f $(DESTDIR)/$(libdir)/pkgconfig/$(NAME).pc; \
	rm -f $(DESTDIR)/$(bindir)/lnctool; \
	rm -rf $(DESTDIR)/$(datadir)/$(NAME)/rnglib/; \
	rm -rf $(DESTDIR)/$(datadir)/$(NAME)/xslt/; \
	rm -f $(DESTDIR)/$(bindir)/lncdatastore;

.PHONY: uninstall-doc
uninstall-doc:
	rm -rf $(DESTDIR)/$(datadir)/$(NAME)/doxygen;

.PHONY: uninstall-pyapi
uninstall-pyapi:
	@cd python; $(PYTHON) ./setup.py uninstall; cd -;

.PHONY: clean clean-all clean-doc clean-rpm
clean:
	rm -rf *.a *.so* .obj $(OBJS) $(BUILT_RNGS) $(LNCTOOL) $(LNCTOOL).install python/build
	$(LIBTOOL) --mode clean rm -f $(LOBJS)
	$(LIBTOOL) --mode clean rm -f $(NAME).la

clean-all: clean clean-doc clean-rpm

clean-doc:
	rm -rf $(DOXYGEN_DIR);

clean-rpm:
	rm -rf $(RPMDIR)
